package com.sk.weichat.ui.groupchat;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sk.weichat.R;
import com.sk.weichat.db.dao.GroupMessageDao;
import com.sk.weichat.helper.DialogHelper;
import com.sk.weichat.ui.base.EditBaseActivity;
import com.sk.weichat.ui.message.multi.InviteVerifyActivity;
import com.sk.weichat.util.AppUtils;
import com.sk.weichat.util.ToastUtil;
import com.sk.weichat.view.ClearEditText;
import com.sk.weichat.view.MaxTextTwoLengthFilter;
import com.suke.widget.SwitchButton;
import com.xuan.xuanhttplibrary.okhttp.HttpUtils;
import com.xuan.xuanhttplibrary.okhttp.callback.BaseCallback;
import com.xuan.xuanhttplibrary.okhttp.result.ObjectResult;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

/**
 * @Author: chenzl
 * @ClassName: RefuseInviteActivity
 * @Description: 拒绝邀请、加群申请
 * @CreateDate: 2019/9/19 14:44
 */
public class RefuseInviteActivity extends EditBaseActivity implements View.OnClickListener {

    private TextView mTitleTv;
    private TextView tv_title_right;
    private ClearEditText edit_refuse_content;
    private TextView tv_input_length;
    private RelativeLayout rt_sbtn;
    private SwitchButton sbtn_accept_invite;
    String packet;//消息id

    private boolean isPersonalInvite;//是否是个人加群申请  ture 是个人  flase 是邀请申请


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_refuse_invite);
        getSupportActionBar().hide();
        packet = getIntent().getStringExtra("VERIFY_MESSAGE_PACKET");
        isPersonalInvite = getIntent().getBooleanExtra("isPersonalInvite",false);
        initView();
    }

    private void initView(){
        findViewById(R.id.iv_title_left).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mTitleTv = findViewById(R.id.tv_title_center);
        mTitleTv.setText(getString(R.string.refuse));
        tv_title_right = findViewById(R.id.tv_title_right);
        tv_title_right.setText(getString(R.string.send));
        tv_title_right.setOnClickListener(this);

        edit_refuse_content = findViewById(R.id.edit_refuse_content);
        tv_input_length = findViewById(R.id.tv_input_length);

        edit_refuse_content.addTextChangedListener(new MyTextWatcher());
        edit_refuse_content.setFilters(new InputFilter[]{new MaxTextTwoLengthFilter(60)});

        rt_sbtn = findViewById(R.id.rt_sbtn);
        sbtn_accept_invite = findViewById(R.id.sbtn_accept_invite);
        if (isPersonalInvite){
            rt_sbtn.setVisibility(View.VISIBLE);
        }else {
            rt_sbtn.setVisibility(View.GONE);
        }
    }

    @Override
    public void onClick(View v) {
        if (v == tv_title_right) {
            if (isPersonalInvite){
                if (sbtn_accept_invite.isChecked()){
                    refusePersonalInvite(packet,edit_refuse_content.getText().toString(),1);
                }else {
                    refusePersonalInvite(packet,edit_refuse_content.getText().toString(),0);
                }
            }else {
                refuseInvite(packet,edit_refuse_content.getText().toString());
            }
        }
    }

    /**
     * @author: czl
     * @description 拒绝邀请加群申请
     * @date: 2019/9/19 14:42
     */
    private void refuseInvite(String msgId,String opinion){
        DialogHelper.showDefaulteMessageProgressDialog(this);

        Map<String, String> params = new HashMap<>();
        params.put("access_token", coreManager.getSelfStatus().accessToken);
        params.put("msgId", msgId);
        params.put("opinion", opinion);
        HttpUtils.post().url(coreManager.getConfig().ROOM_REFUSE_INVITE)
                .params(params).build().execute(new BaseCallback<String>(String.class) {

            @Override
            public void onResponse(ObjectResult<String> result) {
                DialogHelper.dismissProgressDialog();
                if (result.getResultCode()==1){//拒绝邀请加群申请
                    ToastUtil.showToast(RefuseInviteActivity.this,"该邀请已拒绝");
                    GroupMessageDao.getInstance().updateMsgState(coreManager.getSelf().getUserId(),msgId,2);//0 未处理 1 同意 2 拒绝 3过期 4已加入 5 失效 6超员
                    setResult(RESULT_OK);
                    finish();
                }else if (result.getResultCode()==1040324||result.getResultCode()==1040325){//该申请已经处理 或者 该申请已经拒绝
                    ToastUtil.showToast(RefuseInviteActivity.this,result.getResultMsg());
                    ToastUtil.showToast(RefuseInviteActivity.this,result.getResultMsg());
                    setResult(RESULT_OK);
                    finish();

                }else {
                    ToastUtil.showToast(RefuseInviteActivity.this,result.getResultMsg());
                }

            }

            @Override
            public void onError(Call call, Exception e) {
                DialogHelper.dismissProgressDialog();
                ToastUtil.showErrorNet(RefuseInviteActivity.this);
            }
        });
    }

    /**
     * @author: czl
     * @description 拒绝个人加群申请
     * @date: 2019/9/19 15:51
     * @param msgId 拒绝处理的消息id（申请时发送的消息id）
     * @param opinion 拒绝意见
     * @param applyAgain 是否不再接受该用户加群申请：1，表示是；0，否
     */
    private void refusePersonalInvite(String msgId,String opinion,int applyAgain){
        DialogHelper.showDefaulteMessageProgressDialog(this);

        Map<String, String> params = new HashMap<>();
        params.put("access_token", coreManager.getSelfStatus().accessToken);
        params.put("msgId", msgId);
        params.put("opinion", opinion);
        params.put("applyAgain", String.valueOf(applyAgain));

        HttpUtils.post().url(coreManager.getConfig().ROOM_REFUSE_PERSONAL_INVITE)
                .params(params).build().execute(new BaseCallback<String>(String.class) {

            @Override
            public void onResponse(ObjectResult<String> result) {
                DialogHelper.dismissProgressDialog();
                if (result.getResultCode()==1){//拒绝加群申请
                    ToastUtil.showToast(RefuseInviteActivity.this,"该申请已拒绝");
                    setResult(RESULT_OK);
                    finish();
                }else if (result.getResultCode()==1040324||result.getResultCode()==1040325||result.getResultCode()==1040319||result.getResultCode()==1040322){//该申请已经处理 或者 该申请已经拒绝 或者已加入
                    ToastUtil.showToast(RefuseInviteActivity.this,result.getResultMsg());
                    setResult(RESULT_OK);
                    finish();
                }else {
                    ToastUtil.showToast(RefuseInviteActivity.this,result.getResultMsg());
                }
            }

            @Override
            public void onError(Call call, Exception e) {
                DialogHelper.dismissProgressDialog();
                ToastUtil.showErrorNet(RefuseInviteActivity.this);
            }
        });

    }


    private class MyTextWatcher implements TextWatcher {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (s.length() == 0) {
                tv_title_right.setTextColor(getResources().getColor(R.color.color_FFBDC2C8));
                tv_title_right.setEnabled(false);
                tv_input_length.setText("0/" + 60);
            } else {
//                if (!s.equals(content)) {
//                    tv_title_right.setTextColor(getResources().getColor(R.color.deep_purple));
//                    tv_title_right.setEnabled(true);
//                }
                tv_title_right.setTextColor(getResources().getColor(R.color.deep_purple));
                tv_title_right.setEnabled(true);
                tv_input_length.setText(AppUtils.judgeTextLength(s.toString()) + "/" + 60);
            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    }
}
